package com.taomu.app.zongchouhuli.utils.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.utils.entity.OrderExplainEntity;

import java.util.ArrayList;
import java.util.List;

public class OrderDialogBalanceTypeAdp extends RecyclerView.Adapter<OrderDialogBalanceTypeAdp.OrderDialogBalanceTypeHolder>{

    private List<OrderExplainEntity> mData;

    private List<OrderExplainEntity> mSelectData;

    private Context mContext;

    private LayoutInflater inflater;

    public OrderDialogBalanceTypeAdp(Context mContext){
        this.mContext = mContext;
        this.mData = new ArrayList<>();
        this.mSelectData = new ArrayList<>();
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setData(List<OrderExplainEntity> mData){
        this.mData.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    public void setData(List<OrderExplainEntity> mData,OrderExplainEntity data){
        this.mData.clear();
        this.mData.addAll(mData);
        if(data != null){
            mSelectData.clear();
            mSelectData.add(data);
        }
        notifyDataSetChanged();
    }

    public void addData(List<OrderExplainEntity> mData){
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }

    @Override
    public OrderDialogBalanceTypeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view  =inflater.inflate(R.layout.item_dialog_order_balance_type,parent,false);
        return new OrderDialogBalanceTypeHolder(view);
    }

    @Override
    public void onBindViewHolder(OrderDialogBalanceTypeHolder holder, int position) {
        final OrderExplainEntity entity = mData.get(position);
        holder.update(entity);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkSelect(entity);
                if(listener != null){
                    listener.onItemClick(entity);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class OrderDialogBalanceTypeHolder extends RecyclerView.ViewHolder{

        private TextView mNameTv;

        public OrderDialogBalanceTypeHolder(View itemView) {
            super(itemView);
            mNameTv = itemView.findViewById(R.id.item_dialog_order_balance_type_tv);
        }

        public void update(OrderExplainEntity entity){
            mNameTv.setText(entity.getTypeName()+entity.getRadioName());
            if(isSelect(entity)){
                mNameTv.setBackgroundResource(R.drawable.shape_order_select_cornor);
            }else{
                mNameTv.setBackgroundResource(R.drawable.shape_order_unselect_cornor);
            }
        }
    }

    public void checkSelect(OrderExplainEntity entity){
        mSelectData.clear();
        mSelectData.add(entity);
        notifyDataSetChanged();
    }

    public boolean isSelect(OrderExplainEntity entity){
        return mSelectData.contains(entity);
    }

    public List<OrderExplainEntity> getSelectData(){
        return mSelectData;
    }

    private OrderDialogBalanceTypeListener listener;

    public void setOrderDialogBalanceTypeListener(OrderDialogBalanceTypeListener listener){
        this.listener = listener;
    }

    public interface OrderDialogBalanceTypeListener{
        void onItemClick(OrderExplainEntity entity);
    }
}
